// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CourseServiceImp.java

package com.iphaeton.lms.business.course.scorm12;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.business.organize.IUserService;
import com.iphaeton.lms.pojo.course.*;
import com.iphaeton.lms.pojo.note.Note;
import com.iphaeton.lms.pojo.organize.Organize;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;
import org.dom4j.*;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

// Referenced classes of package com.iphaeton.lms.business.course.scorm12:
//            ICourseService

public class CourseServiceImp extends PersistentServiceSupport
    implements ICourseService
{

    public CourseServiceImp()
    {
        user = null;
    }

    public IUserService getUser()
    {
        return user;
    }

    public void setUser(IUserService user)
    {
        this.user = user;
    }

    public Course persistent(Course c)
        throws BusinessException
    {
        Course course = null;
        try
        {
            course = (Course)hbpservice.store(c);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u8BFE\u7A0B\u5BF9\u8C61\u5931\u8D25!");
        }
        return course;
    }

    public Course persistentCommit(Course c)
        throws BusinessException
    {
        Course course = null;
        try
        {
            course = (Course)hbpservice.store(c);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u8BFE\u7A0B\u5BF9\u8C61\u5931\u8D25!");
        }
        return course;
    }

    public UserCourse persistent(UserCourse c)
        throws BusinessException
    {
        UserCourse uc = null;
        try
        {
            uc = (UserCourse)hbpservice.store(c);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u5B66\u5458\u8BFE\u7A0B\u5BF9\u8C61\u5931\u8D25!");
        }
        return uc;
    }

    public UserCourse findUserCourseById(String id)
        throws BusinessException
    {
        UserCourse uc = null;
        try
        {
            uc = (UserCourse)hbpservice.get(id, com.iphaeton.lms.pojo.course.UserCourse.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u901A\u8FC7ID\u67E5\u627E\u7528\u6237\u9009\u8BFE\u8BB0\u5F55\u5931\u8D25!");
        }
        return uc;
    }

    public Course findCourseById(String id)
        throws BusinessException
    {
        Course c = null;
        try
        {
            c = (Course)hbpservice.get(id, com.iphaeton.lms.pojo.course.Course.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u8BFE\u7A0B\u5931\u8D25!");
        }
        return c;
    }

    public List findCourse(String state, String orgId, String sortId)
        throws BusinessException
    {
        List list = null;
        String hql = "from Course c where c.state='" + state + "'";
        if(!"".equals(orgId))
            hql = hql + " and c.organizeId='" + orgId + "'";
        if(!"".equals(sortId))
            hql = hql + " and c.sortId='" + sortId + "'";
        hql = hql + " order by c.name";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u8BFE\u7A0B\u9519\u8BEF\uFF01");
        }
        return list;
    }

    public Pagination findCourse(String state, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(c) from Course c where c.state='" + state + "'";
        String hqlQueryStr = "from Course c where c.state='" + state.trim() + "'order by c.sort,c.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BFE\u7A0B\u67E5\u627E\u51FA\u9519\uFF0C\u627E\u4E0D\u5230\u8BFE\u7A0B!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination findSortCourse(String state, String sort_id, String org_id, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(c) from Course c where c.state='" + state.trim() + "' and c.organizeId='" + org_id + "' ";
        String hqlQueryStr = "from Course c where c.state='" + state.trim() + "' and c.organizeId='" + org_id + "'";
        if(sort_id != null && !"".equals(sort_id))
        {
            selectHql = selectHql + " and c.sortId='" + sort_id + "' ";
            hqlQueryStr = hqlQueryStr + " and c.sortId='" + sort_id + "' ";
        }
        hqlQueryStr = hqlQueryStr + " order by c.sort,c.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BFE\u7A0B\u67E5\u627E\u51FA\u9519\uFF0C\u627E\u4E0D\u5230\u8BFE\u7A0B!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List findPageList(Pagination pageInfo)
    {
        List list = null;
        try
        {
            list = getSpecifiedItems(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    public void assignCourse(Course course, Organize org)
        throws BusinessException
    {
        if(!isOrganizeCourse(course, org))
        {
            String sql = "insert into t_organize_course (course_id,organize_id) values ('" + course.getId() + "','" + org.getId() + "')";
            try
            {
                jdbcpservice.execute(sql);
            }
            catch(PersistenceException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u5206\u914D\u8BFE\u7A0B\u51FA\u9519!");
            }
        }
    }

    public float getPeriod(String user_id, String course_id)
        throws BusinessException
    {
        String hql = "select uc.period from UserCourse uc where uc.userId='" + user_id + "' and uc.courseId='" + course_id + "'";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u8BFE\u7A0B\u5B66\u4E60\u65F6\u95F4\u51FA\u9519!");
        }
        if(list != null && list.size() > 0)
            return ((Float)list.get(0)).floatValue() <= 1.0F ? 1.0F : ((Float)list.get(0)).floatValue();
        else
            return 0.0F;
    }

    public void applyCourse(String courseId, String userId)
        throws BusinessException
    {
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = df.format(time);
        UserCourse uc = new UserCourse();
        uc.setCourseId(courseId);
        uc.setUserId(userId);
        uc.setState(new Integer(2));
        uc.setType(new Integer(1));
        uc.setApplyTime(currentTime);
        try
        {
            hbpservice.store(uc);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7ED9\u7528\u6237 '" + userId + " '\u5206\u914D\u8BFE\u7A0B '" + courseId + " '\u5931\u8D25!");
        }
    }

    public void removeCourseApply(String id)
        throws BusinessException
    {
        String sql1 = "delete from t_user_item where user_course_id='" + id + "'";
        String sql = "delete from t_user_course where id='" + id + "'";
        try
        {
            jdbcpservice.execute(sql1);
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u53D6\u6D88\u7528\u6237\u8BFE\u7A0B\u7533\u8BF7\u5931\u8D25!");
        }
    }

    public void approveCourseApply(String id)
        throws BusinessException
    {
        UserCourse uc = findUserCourseById(id);
        uc.setState(new Integer(1));
        try
        {
            hbpservice.store(uc);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6279\u51C6\u7528\u6237\u8BFE\u7A0B\u7533\u8BF7\u5931\u8D25!");
        }
    }

    public List findCourseSort()
        throws BusinessException
    {
        List list = null;
        String hql = "from CourseSort cs";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230\u8BFE\u7A0B\u5206\u7C7B");
        }
        return list;
    }

    public void addCourseSort(String sort, String comment)
        throws BusinessException
    {
        CourseSort cs = new CourseSort();
        cs.setName(sort);
        if(comment != null && !"".equals(comment))
            cs.setComment(comment);
        try
        {
            hbpservice.store(cs);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u8BFE\u7A0B\u5206\u7C7B\u5931\u8D25\uFF01");
        }
    }

    public CourseSort findCourseSort(String id)
        throws BusinessException
    {
        CourseSort cs = null;
        try
        {
            cs = (CourseSort)hbpservice.get(id, com.iphaeton.lms.pojo.course.CourseSort.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BFE\u7A0B\u5206\u7C7B\u5931\u8D25!");
        }
        return cs;
    }

    public CourseSort updateCourseSort(String id, String name, String comment)
        throws BusinessException
    {
        CourseSort cs = null;
        try
        {
            cs = (CourseSort)hbpservice.get(id, com.iphaeton.lms.pojo.course.CourseSort.class);
            cs.setName(name);
            cs.setComment(comment);
            cs = (CourseSort)hbpservice.store(cs);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7F16\u8F91\u8BFE\u7A0B\u5206\u7C7B\u5931\u8D25!");
        }
        return cs;
    }

    public void deleteCourseSort(String id)
        throws BusinessException
    {
        try
        {
            if(((CourseSort)hbpservice.get(id, com.iphaeton.lms.pojo.course.CourseSort.class)).equals("\u672A\u5206\u7C7B"))
                throw new BusinessException("\u672A\u5206\u7C7B \u662F\u7CFB\u7EDF\u5206\u7C7B\u4E0D\u80FD\u5220\u9664");
            String sort_id = getNotCourseSort();
            List list = getSortCourse(id);
            for(int i = 0; i < list.size(); i++)
            {
                Course c = (Course)list.get(i);
                c.setSortId(sort_id);
                hbpservice.store(c);
            }

            hbpservice.remove(id, com.iphaeton.lms.pojo.course.CourseSort.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u8BFE\u7A0B\u5206\u7C7B\u5931\u8D25!");
        }
    }

    public Course removeCourse(Course c)
        throws BusinessException
    {
        c.setState("p");
        c = persistent(c);
        return c;
    }

    public Document getCourseDocument(String orgId)
        throws BusinessException
    {
        List sort = null;
        List courseList = null;
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("root");
        try
        {
            sort = findCourseSort();
            courseList = findCourse("a", orgId, "");
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        Element allElement = root.addElement("\u6240\u6709\u8BFE\u7A0B");
        for(int i = 0; i < courseList.size(); i++)
        {
            Course ac = (Course)courseList.get(i);
            Element allCourse = allElement.addElement("course");
            allCourse.setText(ac.getName());
            allCourse.addAttribute("id", ac.getId());
        }

        for(Iterator sortIt = sort.iterator(); sortIt.hasNext();)
        {
            CourseSort s = (CourseSort)sortIt.next();
            Element sortElement = root.addElement(s.getName());
            for(Iterator courseIt = courseList.iterator(); courseIt.hasNext();)
            {
                Course c = (Course)courseIt.next();
                if(c.getSortId().equals(s.getId()))
                {
                    Element course = sortElement.addElement("course");
                    course.setText(c.getName());
                    course.addAttribute("id", c.getId());
                }
            }

        }

        return doc;
    }

    public Pagination findSelCourseStu(String sortId, String courseId, String row, int state, int pageSize)
        throws BusinessException
    {
        List list = null;
        String selectHql = "select count(uc) from UserCourse uc where uc.course.state='a'";
        String hqlQueryStr = "from UserCourse uc where uc.course.state='a'";
        if(state == 0)
        {
            selectHql = selectHql + " and uc.state in (1,2) ";
            hqlQueryStr = hqlQueryStr + " and uc.state in (1,2) ";
        } else
        {
            selectHql = selectHql + " and uc.state=" + state;
            hqlQueryStr = hqlQueryStr + " and uc.state=" + state;
        }
        if(sortId != null && !"".equals(sortId))
        {
            selectHql = selectHql + " and uc.course.sortId='" + sortId + "'";
            hqlQueryStr = hqlQueryStr + " and uc.course.sortId='" + sortId + "'";
        }
        if(courseId != null && !"".equals(courseId))
        {
            selectHql = selectHql + " and uc.courseId='" + courseId + "'";
            hqlQueryStr = hqlQueryStr + " and uc.courseId='" + courseId + "'";
        }
        selectHql = selectHql + " and uc.type=1 and (uc.period is null or uc.period=0) ";
        hqlQueryStr = hqlQueryStr + " and uc.type=1 and (uc.period is null or uc.period=0) ";
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by uc." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by uc.course.name";
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6CA1\u6709\u5F85\u6279\u51C6\u7528\u6237!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination courseStudyInfo(String organize_id, String course_id, String sort, String row, int pageSize)
        throws BusinessException
    {
        List list = null;
        String selectHql = "select count(uc) from UserCourse uc where uc.type=1 and uc.state=1 and uc.course.organizeId='" + organize_id + "' ";
        String hqlQueryStr = "from UserCourse uc where uc.type=1 and uc.state=1 and uc.course.organizeId='" + organize_id + "' ";
        if(sort != null && !"".equals(sort))
        {
            selectHql = selectHql + " and uc.course.sortId='" + sort + "'";
            hqlQueryStr = hqlQueryStr + " and uc.course.sortId='" + sort + "'";
        }
        if(course_id != null && !"".equals(course_id))
        {
            selectHql = selectHql + " and uc.course.id='" + course_id + "'";
            hqlQueryStr = hqlQueryStr + " and uc.course.id='" + course_id + "'";
        }
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by uc." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by uc.course.name";
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6CA1\u6709\u5F85\u6279\u51C6\u7528\u6237!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List getCourseItem(Course c)
        throws BusinessException
    {
        String hql = "from Item i where i.courseId='" + c.getId() + "'";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u8BFE\u7A0Bitem\u51FA\u9519!");
        }
        return list;
    }

    public List getOrganizeCourse(Organize org)
        throws BusinessException
    {
        List list = null;
        String sql = "select course.id,course.name from t_course course where course.id in (select course_id from t_organize_course where organize_id='" + org.getId() + "')";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6CA1\u6709\u4E3A\u5F53\u524D\u7528\u6237\u7684\u7EC4\u7EC7\u5206\u914D\u8BFE\u7A0B!");
        }
        return list;
    }

    private boolean isOrganizeCourse(Course c, Organize o)
    {
        boolean flag = false;
        Set set = c.getOrganize();
        if(set != null)
        {
            for(Iterator it = set.iterator(); it.hasNext();)
                if(it.next().equals(o))
                    flag = true;

        }
        return flag;
    }

    public Pagination selectCourse(String id, int pageSize)
        throws BusinessException
    {
        String selectSql = "select count(course_id) as count from t_organize_course where organize_id='" + id + "'";
        List list = null;
        try
        {
            list = jdbcpservice.query(selectSql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u7ED3\u6784\u8BFE\u7A0B\u6570\u91CF\u5931\u8D25!");
        }
        logger.debug(new Integer(list.size()));
        logger.debug(((Map)list.get(0)).get("count"));
        int itemCount = list.size() <= 0 ? 0 : ((Integer)((Map)list.get(0)).get("count")).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        return pageInfo;
    }

    public List selectCourse(String id, String row, int pageSize, int currentPage)
        throws BusinessException
    {
        if(row == null || "".equals(row))
            row = "cname";
        List list = null;
        String querySql1 = "select top " + pageSize + " oc.course_id as id,o.id as oid,c.creater as uname,c.criterion as criterion,c.name as cname,cs.name as sname,o.name as oname";
        querySql1 = querySql1 + " from t_organize_course oc ";
        querySql1 = querySql1 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id inner join t_organize o on o.id=c.organize_id ";
        if(id != null && !"".equals(id))
            querySql1 = querySql1 + " and oc.organize_id='" + id + "'";
        String querySql2 = " and oc.course_id not in (select top " + pageSize * (currentPage - 1) + " oc.course_id from t_organize_course oc " + "inner join t_course c on oc.course_id=c.id and c.state='a' " + "inner join t_course_sort cs on c.sort_id=cs.id " + "inner join t_organize o on o.id=c.organize_id ";
        if(id != null && !"".equals(id))
            querySql2 = querySql2 + " and oc.organize_id='" + id + "' ";
        querySql2 = querySql2 + ") order by " + row;
        String querySql = querySql1 + querySql2;
        try
        {
            list = jdbcpservice.query(querySql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u7ED3\u6784\u8BFE\u7A0B\u5931\u8D25!");
        }
        return list;
    }

    public void removeCourseFromOrg(String organizeId, String courseId)
        throws BusinessException
    {
        String cid[] = courseId.split("&");
        for(int i = 0; i < cid.length; i++)
        {
            String sql = "delete from t_organize_course where organize_id='" + organizeId + "' and course_id='" + cid[i] + "'";
            try
            {
                jdbcpservice.execute(sql);
            }
            catch(PersistenceException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u5220\u9664\u5DF2\u5206\u914D\u7ED9\u7EC4\u7EC7\u7684\u8BFE\u7A0B\u5931\u8D25!");
            }
        }

    }

    public Pagination courseSelectManage(String orgUrl, String userId, int state, int pageSize)
        throws BusinessException
    {
        String selectSql = "select count(course_id) as count from";
        if(state == 1)
            selectSql = selectSql + " t_organize_course oc inner join t_course c on c.id=oc.course_id and c.state='a' inner join t_organize o on o.id=oc.organize_id and '" + orgUrl + "' like o.url" + "+'%'";
        else
        if(state == 2)
            selectSql = selectSql + " t_user_course uc inner join t_course c on c.id=uc.course_id and c.state='a' and uc.state=2 and uc.type=1 and user_id='" + userId + "'";
        else
        if(state == 3)
            selectSql = selectSql + " t_user_course uc inner join t_course c on c.id=uc.course_id and c.state='a' and uc.state=1 and uc.type=1 and user_id='" + userId + "'";
        else
        if(state == 4)
            selectSql = selectSql + " t_user_course uc inner join t_course c on c.id=uc.course_id and c.state='a' and uc.type=1 and uc.user_id='" + userId + "' and (uc.state=1 or uc.state=2) ";
        else
        if(state == 5)
            selectSql = selectSql + " t_organize_course oc inner join t_course c on c.id=oc.course_id inner join t_organize o on o.id=oc.organize_id and '" + orgUrl + "' like o.url" + "+'%' and oc.course_id not in (select course_id from t_user_course uc) ";
        List list = null;
        try
        {
            list = jdbcpservice.query(selectSql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u9009\u8BFE\u6570\u91CF\u5931\u8D25!");
        }
        logger.debug(new Integer(list.size()));
        logger.debug(((Map)list.get(0)).get("count"));
        int itemCount = list.size() <= 0 ? 0 : ((Integer)((Map)list.get(0)).get("count")).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        return pageInfo;
    }

    public List courseSelectManage(String orgUrl, String userId, String row, int state, int pageSize, int currentPage)
        throws BusinessException
    {
        if(row == null || "".equals(row))
            row = "cname";
        List list = null;
        String querySql1 = "";
        String querySql2 = "";
        if(state == 1)
        {
            querySql1 = querySql1 + "select top " + pageSize + " oc.course_id as id,uc.state as state,o.id as oid,c.creater as uname,c.criterion as criterion,c.name as cname,cs.name as sname,o.name as oname";
            querySql1 = querySql1 + " from t_organize_course oc inner join t_course c on oc.course_id=c.id and c.state='a' ";
            querySql1 = querySql1 + " inner join t_course_sort cs on c.sort_id=cs.id ";
            if(orgUrl != null && !"".equals(orgUrl))
                querySql1 = querySql1 + " inner join t_organize o on o.id=oc.organize_id  and '" + orgUrl + "' like o.url" + "+'%'";
            querySql1 = querySql1 + " left join t_user_course uc on uc.course_id=c.id and uc.type=1  and uc.user_id='" + userId + "'";
            querySql2 = querySql2 + " and oc.course_id not in (select top " + pageSize * (currentPage - 1) + " oc.course_id from t_organize_course oc " + "inner join t_course c on oc.course_id=c.id and c.state='a' " + "inner join t_course_sort cs on c.sort_id=cs.id ";
            if(orgUrl != null && !"".equals(orgUrl))
                querySql2 = querySql2 + " inner join t_organize o on o.id=oc.organize_id  and '" + orgUrl + "' like o.url" + "+'%'";
            querySql2 = querySql2 + " left join t_user_course uc on uc.course_id=c.id and uc.type=1  and uc.user_id='" + userId + "'";
            querySql2 = querySql2 + ") order by " + row;
        } else
        if(state == 2)
        {
            querySql1 = querySql1 + "select top " + pageSize + " uc.id as id,oc.course_id as cid,o.id as oid,uc.state as state,c.creater as uname,c.criterion as criterion,c.name as cname,cs.name as sname,o.name as oname";
            querySql1 = querySql1 + " from t_user_course uc inner join t_organize_course oc on uc.course_id=oc.course_id and uc.type=1  ";
            querySql1 = querySql1 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql1 = querySql1 + " inner join t_organize o on o.id=oc.organize_id and uc.user_id='" + userId + "' and uc.state=2 ";
            querySql2 = querySql2 + " and oc.course_id not in (select top " + pageSize * (currentPage - 1) + " oc.course_id from t_user_course uc " + "inner join t_organize_course oc on uc.course_id=oc.course_id  and uc.type=1 ";
            querySql2 = querySql2 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql2 = querySql2 + " inner join t_organize o on o.id=oc.organize_id and uc.user_id='" + userId + "' and uc.state=2 ";
            querySql2 = querySql2 + ") order by " + row;
        } else
        if(state == 3)
        {
            querySql1 = querySql1 + "select top " + pageSize + " uc.id as id,oc.course_id as cid,o.id as oid,uc.state as state,c.creater as uname," + "c.criterion as criterion,c.name as cname,cs.name as sname,o.name as oname,c.hour as hour,uc.period/c.hour*100 as rate";
            querySql1 = querySql1 + " from t_user_course uc inner join t_organize_course oc on uc.course_id=oc.course_id  and uc.type=1 ";
            querySql1 = querySql1 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql1 = querySql1 + " inner join t_organize o on o.id=oc.organize_id and uc.user_id='" + userId + "' and uc.state=1 ";
            querySql2 = querySql2 + " and oc.course_id not in (select top " + pageSize * (currentPage - 1) + " oc.course_id from t_user_course uc inner join t_organize_course oc on uc.course_id=oc.course_id  and uc.type=1 ";
            querySql2 = querySql2 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql2 = querySql2 + " inner join t_organize o on o.id=oc.organize_id and uc.user_id='" + userId + "' and uc.state=1 ";
            querySql2 = querySql2 + ") order by " + row;
        } else
        if(state == 4)
        {
            querySql1 = querySql1 + "select top " + pageSize + " uc.id as id,oc.course_id as cid,o.id as oid,uc.state as state,c.creater as uname,c.criterion as criterion,c.name as cname,cs.name as sname,o.name as oname";
            querySql1 = querySql1 + " from t_user_course uc inner join t_organize_course oc on uc.course_id=oc.course_id  and uc.type=1 ";
            querySql1 = querySql1 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql1 = querySql1 + " inner join t_organize o on o.id=oc.organize_id and '" + orgUrl + "' like o.url" + "+'%' and uc.user_id='" + userId + "' and (uc.state=1 or uc.state=2) ";
            querySql2 = querySql2 + " and oc.course_id not in (select top " + pageSize * (currentPage - 1) + " oc.course_id from t_user_course uc " + "inner join t_organize_course oc on uc.course_id=oc.course_id  and uc.type=1 ";
            querySql2 = querySql2 + " inner join t_course c on oc.course_id=c.id and c.state='a' inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql2 = querySql2 + " inner join t_organize o on o.id=oc.organize_id and '" + orgUrl + "' like o.url" + "+'%' and uc.user_id='" + userId + "' and (uc.state=1 or uc.state=2) ";
            querySql2 = querySql2 + ") order by " + row;
        } else
        if(state == 5)
        {
            querySql1 = querySql1 + "select top " + pageSize + " oc.course_id as id,o.id as oid,c.creater as uname,c.criterion as criterion," + "c.name as cname,cs.name as sname,o.name as oname";
            querySql1 = querySql1 + " from t_organize_course oc ";
            querySql1 = querySql1 + " inner join t_course c on c.id=oc.course_id inner join t_organize o on o.id=oc.organize_id and '" + orgUrl + "' like o.url" + "+'%' and c.state='a' " + "inner join t_course_sort cs on c.sort_id=cs.id ";
            querySql1 = querySql1 + " and oc.course_id not in (select course_id from t_user_course where type=1 )";
            querySql2 = querySql2 + " and oc.course_id not in (select top " + pageSize * (currentPage - 1) + " oc.course_id from t_organize_course oc ";
            querySql2 = querySql2 + " inner join t_course c on oc.course_id=c.id inner join t_organize o on oc.organize_id=o.id and '" + orgUrl + "' like o.url" + "+'%' and c.state='a' " + "inner join t_course_sort cs on c.sort_id=cs.id and oc.course_id not in " + "(select course_id from t_user_course where type=1)";
            querySql2 = querySql2 + ") order by " + row;
        }
        String querySql = querySql1 + querySql2;
        try
        {
            list = jdbcpservice.query(querySql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7EC4\u7EC7\u7ED3\u6784\u8BFE\u7A0B\u5931\u8D25!");
        }
        return list;
    }

    public int queryCourseSequence(String id)
        throws BusinessException
    {
        String sql = "select max(sequence) as sequence from t_item where course_id='" + id + "'";
        List list = null;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BFE\u7A0B\u6700\u5927\u5E8F\u53F7\u65F6\u51FA\u9519: " + e.getMessage() + "!");
        }
        return ((Integer)((Map)list.get(0)).get("sequence")).intValue();
    }

    private String getNotCourseSort()
        throws BusinessException
    {
        String sql = "select id from t_course_sort where name='\u672A\u5206\u7C7B'";
        String id = "";
        try
        {
            id = ((String) (((Map)jdbcpservice.query(sql).get(0)).get("id")));
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2 \u672A\u5206\u7C7B \u8BFE\u7A0B\u5206\u7C7BID\u5931\u8D25!");
        }
        return id;
    }

    private List getSortCourse(String id)
        throws BusinessException
    {
        String hql = "from Course c where c.sortId='" + id + "'";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u6307\u5B9A\u5206\u7C7B\u7684\u8BFE\u7A0B\u5217\u8868\u5931\u8D25!");
        }
        return list;
    }

    public List mySelectedCourse(String userId, String strRow)
        throws BusinessException
    {
        if(strRow == null || "".equals(strRow))
            strRow = "cname";
        final String user_id = userId;
        final String row = strRow;
        List list = null;
        CallableStatementCallback cb =new CallableStatementCallback() {

            public Object doInCallableStatement(CallableStatement cs)
                throws SQLException, DataAccessException
            {
                List list = new LinkedList();
                cs.setString(1, user_id);
                cs.setString(2, row);
                cs.execute();
                ResultSet rs;
                Map map;
                for(rs = cs.getResultSet(); rs.next(); list.add(map))
                {
                    map = new HashMap();
                    map.put("id", rs.getString("id"));
                    map.put("cid", rs.getString("cid"));
                    map.put("uname", rs.getString("uname"));
                    map.put("cname", rs.getString("cname"));
                    map.put("sname", rs.getString("sname"));
                    map.put("oname", rs.getString("oname"));
                    map.put("hour", rs.getString("hour"));
                    map.put("period", rs.getString("period"));
                    map.put("rate", rs.getString("rate"));
                }

                rs.close();
                return list;
            }

        };
        try
        {
            list = (List)jdbcpservice.getSpringJdbcTemplate().execute("{call pro_user_course(?,?)}", cb);
        }
        catch(DataAccessException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u81EA\u9009\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u81EA\u9009\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        return list;
    }

    public List myTrainCourse(String userId, String strRow)
        throws BusinessException
    {
        if(strRow == null || "".equals(strRow))
            strRow = "cname";
        final String user_id = userId;
        final String row = strRow;
        List list = null;
        CallableStatementCallback cb = new CallableStatementCallback() {

            public Object doInCallableStatement(CallableStatement cs)
                throws SQLException, DataAccessException
            {
                List list = new LinkedList();
                cs.setString(1, user_id);
                cs.setString(2, row);
                cs.execute();
                ResultSet rs;
                Map map;
                for(rs = cs.getResultSet(); rs.next(); list.add(map))
                {
                    map = new HashMap();
                    map.put("id", rs.getString("id"));
                    map.put("cid", rs.getString("cid"));
                    map.put("uname", rs.getString("uname"));
                    map.put("cname", rs.getString("cname"));
                    map.put("sname", rs.getString("sname"));
                    map.put("tname", rs.getString("tname"));
                    map.put("hour", rs.getString("hour"));
                    map.put("period", rs.getString("period"));
                    map.put("rate", rs.getString("rate"));
                }

                rs.close();
                return list;
            }

        };
        try
        {
            list = (List)jdbcpservice.getSpringJdbcTemplate().execute("{call pro_train_course(?,?)}", cb);
        }
        catch(DataAccessException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u57F9\u8BAD\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5B66\u5458\u57F9\u8BAD\u8BFE\u7A0B\u65F6\u51FA\u9519: " + e.getMessage());
        }
        return list;
    }

    public Document getCourseSortTree()
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = findCourseSort();
        for(int i = 0; i < list.size(); i++)
        {
            CourseSort cs = (CourseSort)list.get(i);
            Element csElement = element.addElement("tree");
            csElement.addAttribute("text", cs.getName());
            csElement.addAttribute("action", "../../courseTxAction.do?method=findSortCourse&sortId=" + cs.getId());
            csElement.addAttribute("icon", "../../images/xtree/course1.gif");
            csElement.addAttribute("target", "operateFrame");
            csElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public Document getCourseTree(String id, String type)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = findCourseSort();
        for(int i = 0; i < list.size(); i++)
        {
            CourseSort cs = (CourseSort)list.get(i);
            Element csElement = element.addElement("tree");
            csElement.addAttribute("text", cs.getName());
            List courseList = findCourse("a", id, cs.getId());
            if(courseList != null && courseList.size() > 0)
                csElement.addAttribute("src", "../../courseTxAction.do?method=sortCourseTree&id=" + cs.getId() + "&type=" + type);
            if(type.equals("report"))
                csElement.addAttribute("action", "../../courseTxAction.do?method=courseStudyInfo&course_sort=" + cs.getId());
            else
            if(type.equals("select"))
                csElement.addAttribute("action", "../../courseTxAction.do?method=findSelCourseStu&state=2&sortId=" + cs.getId());
            else
            if(type.equals("note"))
                csElement.addAttribute("action", "../../noteTxAction.do?method=courseNote&sortId=" + cs.getId());
            else
            if(type.equals("soul"))
                csElement.addAttribute("action", "../../noteTxAction.do?method=courseSoulNote&sortId=" + cs.getId());
            csElement.addAttribute("icon", "../../images/xtree/course1.gif");
            csElement.addAttribute("target", "operateFrame");
            csElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public Document getSortCourseTree(String organize_id, String id, String type)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = findCourse("a", organize_id, id);
        for(int i = 0; i < list.size(); i++)
        {
            Course c = (Course)list.get(i);
            Element cElement = element.addElement("tree");
            cElement.addAttribute("text", c.getName());
            if(type.equals("report"))
                cElement.addAttribute("action", "../../courseTxAction.do?method=courseStudyInfo&course_id=" + c.getId());
            else
            if(type.equals("select"))
                cElement.addAttribute("action", "../../courseTxAction.do?method=findSelCourseStu&state=2&courseId=" + c.getId());
            else
            if(type.equals("note"))
                cElement.addAttribute("action", "../../noteTxAction.do?method=courseNote&courseId=" + c.getId());
            else
            if(type.equals("soul"))
                cElement.addAttribute("action", "../../noteTxAction.do?method=courseSoulNote&courseId=" + c.getId());
            cElement.addAttribute("icon", "../../images/xtree/course.gif");
            cElement.addAttribute("target", "operateFrame");
            cElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public int findApplyUser(String org_url, int state)
        throws BusinessException
    {
        int count = 0;
        String hql = "select count(uc) from UserCourse uc where uc.user.organize.url like '" + org_url + "%' and state=" + state + " and type=1";
        try
        {
            count = ((Integer)hbpservice.query(hql).get(0)).intValue();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u9009\u8BFE\u7528\u6237\u6570\u76EE\u51FA\u9519!");
        }
        return count;
    }

    public List findCourseLabel(String id, String row)
        throws BusinessException
    {
        String hql = "from Label l where l.stuCourseId='" + id + "'";
        if(row != null && !"".equals(row))
            hql = hql + " order by l." + row;
        else
            hql = hql + " order by l.createTime";
        List list;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u4E66\u7B7E\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public void addLabel(Label label)
        throws BusinessException
    {
        try
        {
            hbpservice.store(label);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u4E66\u7B7E\u65F6\u51FA\u9519!");
        }
    }

    public Label findLabelById(String id)
        throws BusinessException
    {
        Label label;
        try
        {
            label = (Label)hbpservice.get(id, com.iphaeton.lms.pojo.course.Label.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u901A\u8FC7id\u67E5\u627E\u4E66\u7B7E\u65F6\u51FA\u9519!");
        }
        return label;
    }

    public void removeLabel(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.course.Label.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u4E66\u7B7E\u65F6\u51FA\u9519!");
        }
    }

    public Note findNoteById(String id)
        throws BusinessException
    {
        Note note;
        try
        {
            note = (Note)hbpservice.get(id, com.iphaeton.lms.pojo.note.Note.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u7B14\u8BB0\u65F6\u51FA\u9519!");
        }
        return note;
    }

    public void removeNote(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.note.Note.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u7B14\u8BB0\u65F6\u51FA\u9519!");
        }
    }

    public List findCourseNote(String course_id, String user_id)
        throws BusinessException
    {
        String hql = "from Note n where n.course_id='" + course_id + "' and n.create_id='" + user_id + "'";
        List list;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8BFE\u7A0B\u7B14\u8BB0\u5217\u8868\u65F6\u51FA\u9519!");
        }
        return list;
    }

    private static Logger logger;
    IUserService user;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */
    static Class class$2; /* synthetic field */
    static Class class$3; /* synthetic field */
    static Class class$4; /* synthetic field */
    static Class class$5; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.course.scorm12.CourseServiceImp.class.getName());
    }
}
